package whf.framework.web.tag;

import java.lang.reflect.Array;

import javax.servlet.jsp.JspException;

import whf.framework.i18n.ApplicationResource;
import whf.framework.meta.Meta;
import whf.framework.security.UserContext;
import whf.framework.security.service.PermissionService;
import whf.framework.security.service.PermissionServiceImp;
import whf.framework.util.StringUtils;
import whf.framework.web.Constants;
import whf.framework.web.WebContextHolder;
import whf.framework.web.tag.ext.TagConstants;

/**
 * 功能按钮
 * 可以根据当前用户的权限决定该功能按钮是否显示
 * @author wanghaifeng
 * @create Aug 26, 2006 12:27:18 PM
 * 
 */
public class FunctionButtonTag extends HtmlTag{
	/**
	 * 操作的目标对象
	 * @property String:target
	 */
	private String target;
	/**
	 * 绑定Struts功能名称（方法名称），必须
	 * @property String:functionName
	 */
	private String operation;
	
	/**
	 * js合法性检查方法，由外部实现，非必须
	 * @property String:validationFunction
	 */
	private String validationFunction;
	
	private String[] validationFunctionParams;

	/**
	 * 缺省的是select.jsp钟的submitForm
	 * @property String:submitFunction
	 */
	private String submitFunction;
	
	private String[] submitFunctionParams;
	
	private String valueKey;
	/**
	 * @param functionName The functionName to set.
	 */
	public void setOperation(String functionName) {
		this.operation = functionName;
	}

	/**
	 * @param validationFunction The validationFunction to set.
	 */
	public void setValidationFunction(String validationFunction) {
		this.validationFunction = validationFunction;
	}
	
	/**
	 * @param params The validationFunctionParams to set.
	 */
	public void setValidationFunctionParams(Object params) {
		if(params == null) return;
		if(params.getClass().isArray()){
			this.validationFunctionParams = new String[Array.getLength(params)];
			for(int i=0; i< this.validationFunctionParams.length; i++){
				this.validationFunctionParams[i] = (String)Array.get(params, i);
			}
		} else {
			this.validationFunctionParams = StringUtils.split(params.toString(), ";");
		}
	}

	public void setSubmitFunction(String func){
		this.submitFunction = func;
	}

	/**
	 * @param params The submitFunctionParams to set.
	 */
	public void setSubmitFunctionParams(Object params) {
		if(params == null) return;
		if(params.getClass().isArray()){
			this.submitFunctionParams = new String[Array.getLength(params)];
			for(int i=0; i< this.submitFunctionParams.length; i++){
				this.submitFunctionParams[i] = (String)Array.get(params, i);
			}
		} else {
			this.submitFunctionParams = StringUtils.split(params.toString(), ";");
		}
	}

	/**
	 * @param valueKey The valueKey to set.
	 */
	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}

	/**
	 * @modify wanghaifeng Aug 28, 2006 11:10:18 PM
	 * @param t
	 */
	public void setTarget(String t){
		this.target = t;
	}
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {
		//权限判断
		UserContext uc = WebContextHolder.getWebContext().getUserContext();
		if(StringUtils.isEmpty(target)){
			target = (String)pageContext.getAttribute(Constants.FUNC_TARGET_TYPE);
		}
		if(StringUtils.isEmpty(target)){
			target = (String)request().getAttribute(Constants.FUNC_TARGET_TYPE);
		}
		if(StringUtils.isEmpty(target)){
			Meta meta = (Meta)request().getAttribute(Constants.STRUTS_META);	//一般使用这个，与Struts上下文结合起来
			if(meta != null){
				target = meta.getModelClass().getName();
			}
		}
		if(uc.hasFunctionPermission(target, this.operation)){
			StringBuilder js= new StringBuilder();
			if(!StringUtils.isEmpty(this.validationFunction)){
				if(this.validationFunctionParams != null && this.validationFunctionParams.length > 0){
					this.validationFunction += "(";
					for(int i=0; i< this.validationFunctionParams.length; i++){
						String param = this.validationFunctionParams[i];
						if(i == 0){
							this.validationFunction += param;
						} else {
							this.validationFunction += ","+param;
						}
					}
					this.validationFunction += ")";
				}
				if(this.validationFunction.indexOf("(") < 0) this.validationFunction += "(";
				if(this.validationFunction.indexOf(")") < 0) this.validationFunction += ")";
				js.append("if(!"+this.validationFunction+") ").append(" return ;");
			}
			if(StringUtils.isEmpty(this.submitFunction)){
				this.submitFunction = "submitForm";
			}
			js.append(this.submitFunction).append("('").append(this.operation);
			if(this.submitFunctionParams != null){
				for(String param: this.submitFunctionParams){
					js.append(",").append(param);
				}
			}
			js.append("')");
			super.setOnclick(js.toString());
			super.setAttribute("type", "BUTTON");
			if(!StringUtils.isEmpty(this.valueKey)){
				String value = ApplicationResource.get(this.valueKey);
				if(!StringUtils.isEmpty(value)){
					super.setValue(value);
				}
			}
			if(StringUtils.isEmpty(super.getAttribute(TagConstants.ATTRIBUTE_VALUE))){	//从数据库中获取value
				try{
					PermissionService service = PermissionServiceImp.getPermissionService();
					String value = service.findByTargetOperation(target, this.operation).getName();
					super.setValue(value);
				}catch(Exception ignore){
					//
				}
			}
			super.println(super.getOpenTag("INPUT"));
			super.print(super.getCloseTag("INPUT"));
		}
		this.operation = null;
		this.valueKey = null;
		this.validationFunction = null;
		this.validationFunctionParams = null;
		this.submitFunction = null;
		this.submitFunctionParams = null;
		super.release();
		return SKIP_BODY;
	}
	
}
